#import "iPhotoGalleryAppDelegate.h"
#import "IPhotoGallery.h"
#import "AsyncNet.h"

@implementation iPhotoGalleryAppDelegate

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
    window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [window makeKeyAndVisible];
    photoGallery = [[IPhotoGallery alloc] initWithDelegate:self];
    [window addSubview:photoGallery.view];
}

- (void)dealloc
{
    [photoGallery release];
    [window release];
    [super dealloc];
}

- (void)asyncPhotoRequestSucceeded:(NSData *)data
    userInfo:(NSDictionary *)userInfo
{
    int index = [[userInfo objectForKey:@"index"] intValue];
    UIImage *photo = [UIImage imageWithData:data];
    if ([[userInfo objectForKey:@"type"] isEqualToString:@"thumb"])
        [photoGallery fetchedPhotoThumb:photo atIndex:index];
    else
        [photoGallery fetchedPhoto:photo atIndex:index];
}

- (void)asyncPhotoRequestFailed:(NSError *)error
    userInfo:(NSDictionary *)userInfo
{
    int index = [[userInfo objectForKey:@"index"] intValue];
    UIImage *photo = nil; // might set some default photo depicting error
    if ([[userInfo objectForKey:@"type"] isEqualToString:@"thumb"])
        [photoGallery fetchedPhotoThumb:photo atIndex:index];
    else
        [photoGallery fetchedPhoto:photo atIndex:index];
}

#pragma mark IPhotoGalleryDelegate

- (int)iPhotoGalleryPhotosCount:(IPhotoGallery *)iPhotoGallery
{
    return 30;
}

- (void)iPhotoGallery:(IPhotoGallery *)iPhotoGallery
    fetchPhotoThumbAtIndex:(int)index
{
    // some images are fetched from the internet asynchronously, some are read
    // from file
    if ((index % 5) == 0)
    {
        NSMutableDictionary *userInfo = [[NSMutableDictionary alloc]
                                         initWithCapacity:2];
        [userInfo setObject:[NSNumber numberWithInt:index] forKey:@"index"];
        [userInfo setObject:@"thumb" forKey:@"type"];
        [[AsyncNet instance]
         addRequestForUrl:[NSString stringWithFormat:
          @"http://iphotogallery.googlecode.com/svn/tags/0.1/Resources/Photo%d.jpg",
          (index % 3) + 1]
         successTarget:self
         successAction:@selector(asyncPhotoRequestSucceeded:userInfo:)
         failureTarget:self
         failureAction:@selector(asyncPhotoRequestFailed:userInfo:)
         userInfo:userInfo];
        [userInfo release];
    }
    else
    {
        UIImage *photo = [UIImage imageNamed:[NSString stringWithFormat:
                                              @"Photo%d.jpg", (index % 3) + 1]];
        [iPhotoGallery fetchedPhotoThumb:photo atIndex:index];
    }
}

- (void)iPhotoGallery:(IPhotoGallery *)iPhotoGallery
    fetchPhotoAtIndex:(int)index
{
    // some images are fetched from the internet asynchronously, some are read
    // from file
    if ((index % 5) == 0)
    {
        NSMutableDictionary *userInfo = [[NSMutableDictionary alloc]
                                         initWithCapacity:2];
        [userInfo setObject:[NSNumber numberWithInt:index] forKey:@"index"];
        [userInfo setObject:@"full" forKey:@"type"];
        [[AsyncNet instance]
         addRequestForUrl:[NSString stringWithFormat:
          @"http://iphotogallery.googlecode.com/svn/tags/0.1/Resources/Photo%d.jpg",
          (index % 3) + 1]
         successTarget:self
         successAction:@selector(asyncPhotoRequestSucceeded:userInfo:)
         failureTarget:self
         failureAction:@selector(asyncPhotoRequestFailed:userInfo:)
         userInfo:userInfo];
        [userInfo release];
    }
    else
    {
        UIImage *photo = [UIImage imageNamed:[NSString stringWithFormat:
                                              @"Photo%d.jpg", (index % 3) + 1]];
        [iPhotoGallery fetchedPhoto:photo atIndex:index];
    }
}

@end
